<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="学校名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学校名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:school:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:school:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:school:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edu:school:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="schoolList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="学校名称" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="学校地址" align="center" prop="address" :show-overflow-tooltip="true"/>
      <el-table-column label="联系人员" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" :show-overflow-tooltip="true"/>
      <el-table-column label="可上传数" align="center" prop="uploadNum" />
      <el-table-column label="可推荐数" align="center" prop="recommendNum" />
      <el-table-column label="已发数" align="center" prop="releaseNum"/>
      <el-table-column label="获赞数" align="center" prop="zanNum"/>
      <el-table-column label="粉丝数" align="center" prop="fansNum"/>
      <el-table-column label="注册时间" align="center" prop="createTime" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:school:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['edu:school:remove']"-->
<!--          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-key"
            @click="handleReset(scope.row)"
            v-hasPermi="['edu:school:edit']"
          >重置密码</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleGet(scope.row)"
            v-hasPermi="['edu:school:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教育-学校信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row v-if="check== 3||check == 1">
          <el-col :span="12" >
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学校地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入学校地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row  v-if="check== 3||check == 1">
          <el-col :span="12">
            <el-form-item label="联系人员" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入联系人员" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row  v-if="check== 3||check == 1">
          <el-col :span="12">
            <el-form-item label="可推荐" prop="recommendNum">
              <el-input-number v-model="form.recommendNum" placeholder="请输入推荐数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可发数" prop="uploadNum">
              <el-input-number v-model="form.uploadNum" placeholder="请输入可发数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" v-if="check== 3">
            <el-form-item label="账号" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入账号名" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="check== 3||check == 2">
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入密码" type="password"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status" placeholder="请选择状态" >
                <el-radio label="0" value="0">启用</el-radio>
                <el-radio label="1" value="1">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="学校详情" :visible.sync="schoolOpen" width="400px" append-to-body>
      <el-image :src="imgUrl+form.headPic" style="height: 100px;width: 100px;text-align: center;"></el-image>

      <el-descriptions :column="1">
        <el-descriptions-item label="学校名称"  label-width="40">
        <span>{{form.name}}</span>
      </el-descriptions-item>
        <el-descriptions-item label="联系人" label-width="40">
          <span>{{ form.contactPerson }}</span>
        </el-descriptions-item>
        <el-descriptions-item  label="招生电话" label-width="40">
          <span>{{ form.phone }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="联系电话" label-width="40">
          <span>{{ form.contactPhone }}</span>
        </el-descriptions-item>

        <el-descriptions-item  label="地址"  label-width="40">
          <span>{{ form.address }}</span>
        </el-descriptions-item>

        <el-descriptions-item label="学校简介" label-width="40">
          <span>{{ form.introduce }}</span>
        </el-descriptions-item>

      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listSchool, getSchool, delSchool, addSchool, updateSchool, exportSchool } from "@/api/forum/school";
import { resetUserPwd } from "@/api/system/user"
export default {
  name: "School",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 教育-学校信息表格数据
      schoolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        headPic: undefined,
        address: undefined,
        introduce: undefined,
        phone: undefined,
        recommendNum: undefined,
        uploadNum: undefined,
        contactPerson: undefined,
        contactPhone: undefined
      },
      //  表单校验
      rules: {
        name:[
          {required: true, message: "请输入学校名称", trigger: "blur"}
        ],
        address:[
          {required: true, message: "请输入学校地址", trigger: "blur"}
        ],
        introduce: [
          {required: true, message: "请输入学校简介", trigger: "blur"}
        ],
        phone:[
          {required :true,message:"请输入招生电话",trigger:'blur'},
          {tel:true,message:"请输入正确的招生电话",trigger:'blur'}
        ],
        contactPerson:[
          {required: true, message: "请输入联系人", trigger: "blur"}
        ],
        contactPhone:[
          {required: true, message: "请输入联系电话", trigger: "blur"},
          {tel:true,message:"请输入正确的联系电话",trigger:'blur'}
        ],
        userName:[
          {required: true, message: "请输入登录账号名", trigger: "blur"}
        ],
        password:[
          {required: true, message: "请输入登录密码", trigger: "blur"}
        ],
        recommendNum:[
          {
            validator: (rule, value, callback) => {
              if (value !== '' && value !== undefined) {
                if ((/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value) === false ) {
                  callback(new Error('请填写不小于0的数字'))
                } else {
                  callback()
                }
              } else {
                callback()
              }
            },
            trigger: 'change'
          }
        ],
        uploadNum: [
          {
            validator: (rule, value, callback) => {
              if (value !== ''&& value !== undefined) {
                if ((/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value) === false) {
                  callback(new Error('请填写不小于0的数字'))
                } else {
                  callback()
                }
              } else {
                callback()
              }
            },
            trigger: 'change'
          }
        ]

      },
      // 表单参数
    form: {

      },
    schoolOpen:false,
      imgUrl:process.env.VUE_APP_BASE_API,
      check:0, //1修改 2重置密码
      loading:false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    statusFormat(row){
      if(row.status == 0){
        return "启用"
      }else if(row.status == 1){
        return "禁用"
      }
    },
    /** 重置密码 **/
    handleReset(row){
      this.$prompt('请输入"' + row.userName + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      }).then(({ value }) => {
        resetUserPwd(row.userId, value).then(response => {
          if (response.code === 200) {
            this.msgSuccess("修改成功，新密码是：" + value);
          }
        });
      }).catch(() => {});
    },
    /** 查询教育-学校信息列表 */
    getList() {
      this.loading = true;
      listSchool(this.queryParams).then(response => {
        this.schoolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: undefined,
        headPic: undefined,
        address: undefined,
        introduce: undefined,
        phone: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        recommendNum: undefined,
        uploadNum: undefined,
        contactPerson: undefined,
        contactPhone: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleGet(row){
      this.reset();
      const deptId = row.deptId || this.ids;
      getSchool(deptId).then(response => {
        this.form = response.data;
        this.schoolOpen = true;
        this.title = "学校信息";
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deptId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.check = 3;
      this.title = "添加教育-学校信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.check = 1;
      const deptId = row.deptId || this.ids
      getSchool(deptId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教育-学校信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      if(this.loading ){
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          if(this.check == 1){
            updateSchool(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.check = 0;
                this.loading = false;
              }
            });
          }else if(this.check == 3){
            addSchool(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.check = 0;
                this.loading = false;
              }
            });
          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deptIds = row.deptId || this.ids;
      this.$confirm('是否确认删除教育-学校信息编号为"' + deptIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSchool(deptIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教育-学校信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSchool(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
